package controll;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
//import javax.servlet.ServletContext;
//import javax.servlet.http.HttpServletResponse;


import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author MARA NETBOOK
 */
public class UploadArquivo {
	private String diretorio;
	private String caminho;
	private byte[] arquivo;
	private String nome;

	public UploadArquivo() {
	}

	public String getDiretorio() {
		return this.diretorio;
	}

	public void setDiretorio(String diretorio) {
		this.diretorio = diretorio;
	}

	public byte[] getArquivo() {
		return arquivo;
	}

	public void setArquivo(byte[] arquivo) {
		this.arquivo = arquivo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getRealPath() {
		/*
		 * ExternalContext externalContext = FacesContext.getCurrentInstance()
		 * .getExternalContext(); HttpServletResponse response =
		 * (HttpServletResponse) externalContext .getResponse(); FacesContext
		 * aFacesContext = FacesContext.getCurrentInstance(); ServletContext
		 * context = (ServletContext) aFacesContext
		 * .getExternalContext().getContext(); return context.getRealPath("/");
		 */
		ExternalContext context = FacesContext.getCurrentInstance()
				.getExternalContext();
		return context.getRealPath("/View/Logos/");
	}

	public void fileUpload(FileUploadEvent event, String type, String diretorio) {
		try {
			
			//**********************
			this.nome = new java.util.Date().getTime() + type;
			System.out.println(getRealPath() + diretorio);
			this.caminho = getRealPath() + diretorio + getNome();
			this.arquivo = event.getFile().getContents();
			File file = new File(getRealPath() + diretorio);
			file.mkdirs();
		} catch (Exception ex) {
			System.out.println("Erro no upload do arquivo" + ex);
		}
	}

	public void gravar() {
		try {
			FileOutputStream fos;
			System.out.println("CAMINHO: " + this.caminho);
			fos = new FileOutputStream(this.caminho);

			fos.write(this.arquivo);
			fos.close();
		} catch (Exception ex) {
			Logger.getLogger(UploadArquivo.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}
	
	private String destination = "/View/Logos/";

	public void upload(FileUploadEvent event) {
		FacesMessage msg = new FacesMessage("Success! ", event.getFile()
				.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
		// Do what you want with the file
		try {
			copyFile(event.getFile().getFileName(), event.getFile()
					.getInputstream());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void copyFile(String fileName, InputStream in) {
		try {
			ExternalContext context = FacesContext.getCurrentInstance()
					.getExternalContext();
			String Endereco = context.getRealPath(destination);
			System.out.print(Endereco
					+ "/" + fileName);
			// write the inputStream to a FileOutputStream
			OutputStream out = new FileOutputStream(new File(Endereco
					+ "/" + fileName));

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = in.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}

			in.close();
			out.flush();
			out.close();

			System.out.println("New file created!");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}